package engine

import "time"

type DurationClock interface {
	SinceStart() time.Duration
}

type TimeLerper struct {
	clock    DurationClock
	min, max float64
	duration time.Duration
}

func NewTimeLerper(c DurationClock) *TimeLerper {
	return &TimeLerper{clock: c}
}

func (lerper *TimeLerper) SetBounds(min, max float64) {
	lerper.min = min
	lerper.max = max
}

func (lerper *TimeLerper) SetMaxDuration(d time.Duration) {
	lerper.duration = d
}

func (lerper *TimeLerper) Interpolate() float64 {
	ticked := lerper.clock.SinceStart()
	progress := ticked.Seconds() / lerper.duration.Seconds()
	interval := lerper.max - lerper.min
	return lerper.clamp(lerper.min + interval*progress)
}

func (lerper *TimeLerper) clamp(value float64) float64 {
	if value < lerper.min {
		return lerper.min
	}
	if value > lerper.max {
		return lerper.max
	}
	return value
}
